#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>

#include "tools.h"

extern int g_debug;

void xx_msg(int level, const char *msg, ...)
{
	va_list ap;
	char	string[1024];
	if ( g_debug == 0 || g_debug < level) {
		return;
	}

	va_start(ap, msg);
	vsnprintf(string, sizeof(string), msg, ap);
	va_end(ap);
	printf("x_x:    %s", string);
}

int  check_torrent_name(const char *filename)
{
	int			  len;

	if ( NULL == filename ) {
		xx_dbg("Input para Invalid\n");
		xx_assert(0);
		return -1;
	}

	len = strlen(filename);
	if ( len < 9 ) {
		return -1;
	}

	if ( strcmp(&filename[len - 8], ".torrent") ) {
		return -1;
	}

	return 0;
}

void get_rand_string(int min, int max, int len, char *str)
{
	int			i = 0;

	if ( NULL == str ) {
		xx_dbg("Input para Invalid\n");
		xx_assert(0);
		return;
	}

	srand((unsigned)time(NULL));
//	srand((int)getpid(NULL));
	while ( i < len ) {
		str[i] = min + rand()%(max - min); 
		i++;
	}
}

